<?php
/**
 * Created by PhpStorm.
 * User: ytl003
 * Date: 2018/10/8
 * Time: 下午7:22
 */

namespace app\api\controller;


use app\api\service\TokenService;
use app\api\validate\FileValidate;
use app\api\validate\ImageValidate;
use app\lib\exception\BaseException;
use app\lib\exception\UploadException;
use think\Exception;

class UploadFile
{
    /**
     * @return array
     * @throws BaseException
     * @route('upload/img')
     */
    public function img()
    {
        TokenService::getCurrentUid();
        (new ImageValidate())->goFileCheck();
        $file = request()->file('image');
        $info = $file->move('../public/upload');
        if (!$info) {
            throw new UploadException();
        }
        return [
            'msg' => '成功',
            'code' => 0,
            'data' => $info->getSaveName()
        ];
    }

    /**
     * @return array
     * @throws UploadException
     * @throws \app\lib\exception\ParameterException
     * @throws \app\lib\exception\PermissionException
     * @route('upload/file')
     */
    public function file(){
        TokenService::needAdminPermission();
        TokenService::needWebPermission();
        (new FileValidate())->goFileCheck();
        $file = request()->file('file');
        $info = $file->move('../public/upload');
        if (!$info) {
            throw new UploadException();
        }
        return [
            'msg' => '成功',
            'code' => 0,
            'data' => $info->getSaveName()
        ];
    }
}